/**
 * The MIT License (MIT)
 *
 * Copyright (c) 2018 Thomas Cashman
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package org.mini2Dx.miniscript.gradle;

import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.JavaFile;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.TypeSpec;
import org.mini2Dx.miniscript.core.GeneratedClasspathScriptProvider;

import javax.lang.model.element.Modifier;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MiniscriptStubTask extends MiniscriptTask {
	private File packageDir;

	@Override
	protected void process(List<CompilerInputFile> inputFiles) throws IOException {
		packageDir = new File(outputDir, outputPackage.replace('.', '/'));

		for(CompilerInputFile inputFile : inputFiles) {
			generateScriptStub(inputFile);
		}

		generateScriptProvider(inputFiles);
	}

	private void generateScriptStub(CompilerInputFile inputFile) throws IOException {
		TypeSpec classSpec = TypeSpec.classBuilder(inputFile.getOutputClassName())
				.addModifiers(Modifier.PUBLIC)
				.build();

		final String scriptPackage = inputFile.getOutputPackageName(outputPackage);

		JavaFile javaFile = JavaFile.builder(scriptPackage, classSpec)
				.addFileComment("Auto-generated by miniscript-gradle-plugin")
				.build();

		javaFile.writeTo(outputDir);
	}

	private void generateScriptProvider(List<CompilerInputFile> inputFiles) throws IOException {
		final String outputClassName = outputClass.get().substring(outputClass.get().lastIndexOf('.') + 1);

		MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder("getGeneratedScripts")
				.addModifiers(Modifier.PUBLIC)
				.returns(Map.class)
				.addStatement("final $T result = new $T()", Map.class, HashMap.class);

		for(CompilerInputFile inputFile : inputFiles) {
			final String scriptPackage = inputFile.getOutputPackageName(outputPackage);
			final ClassName scriptClassName = ClassName.get(scriptPackage, inputFile.getOutputClassName());
			final String path;
			if(getPrefixWithRoot().getOrElse(false)) {
				path = scriptsDir.get().getAsFile().getName() + "/" + inputFile.getInputScriptRelativeFilename().replace('\\', '/');
			} else {
				path =  inputFile.getInputScriptRelativeFilename().replace('\\', '/');
			}
			methodBuilder = methodBuilder.addStatement("result.put($S, new $T())", path, scriptClassName);
		}

		methodBuilder = methodBuilder.addStatement("return result");
		final MethodSpec methodSpec = methodBuilder.build();

		final TypeSpec classSpec = TypeSpec.classBuilder(outputClassName)
				.addModifiers(Modifier.PUBLIC)
				.superclass(GeneratedClasspathScriptProvider.class)
				.addMethod(methodSpec)
				.build();

		JavaFile javaFile = JavaFile.builder(outputPackage, classSpec)
				.addFileComment("Auto-generated by miniscript-gradle-plugin")
				.build();

		javaFile.writeTo(outputDir);
	}
}
